<?php

namespace app\common\model;

use app\common\model\Question;
use think\Model;

class Api extends Model
{
    public function up($data){
        if($this->save($data,['Id'=>1])){
            return true;
        }
        return false;
    }
    public function get_Token(){
        return $this->where('Id',1)->find()['token'];
    }
    //用户查看答案操作
    public function get_ask($que_id){
        $u = new User();
        $user = $u->getinfo();
        $q = new Question();
        $question = $q->getone($que_id);
        $data = ['code'=>0,'answer'=>'','msg'=>'成功'];

        if($question['price'] < 1){
            //免费题目 直接返回答案
            $data = ['code'=>1,'answer'=>$question['answer'],'msg'=>'试题免费'];
            return $data;
        }
        //栏目是否免费
        if($question['profile']['type'] == 1){
            $data = ['code'=>1,'answer'=>$question['answer'],'msg'=>'栏目免费'];
            return $data;
        }
        //如果不是免费题目，检测用户是否是vip
        if($user['grade'] == 1){
            //vip 直接返回答案 检测是否到期
            $data = ['code'=>1,'answer'=>$question['answer'],'msg'=>'VIP免费'];
            return $data;
        }
        //不是vip 检测余额是否足够
        if($user['balance'] < $question['price']){
            $data = ['code'=>2,'answer'=>'','msg'=>'账号积分余额不足'];
            return $data;
        }
        //扣除余额 之后返回答案
        $user->setDec('balance',$question['price']);
        $data = ['code'=>1,'answer'=>$question['answer'],'msg'=>'购买成功'];
        return $data;
    }
}
